package com.situ.mall.controller;

import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.Draw;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.IDrawService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/draw")
public class DrawController {
    @Autowired
    private IDrawService drawService;

    @RequestMapping("/drawNumber")
    @ResponseBody
    public Result drawNumber(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Draw draw = drawService.drawNumber(userId);
        return Result.ok(draw);
    }
    @RequestMapping("/reduceDrawNumber")
    @ResponseBody
    public Result reduceDrawNumber(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Draw draw = drawService.drawNumber(userId);
        if (draw.getDrawNumber() > 0) {
            drawService.reduceDrawNumber(userId);
            return Result.ok("抽奖成功！");
        } else {
            return Result.error("您已无抽奖次数，请去下单！");
        }
    }
}
